package b3.scene.home
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.card.CardItem;
	import b3.widget.home.*;
	import b3.widget.home.bottom.*;
	import b3.widget.home.social.*;
	import b3.widget.home.top.*;
	import b3.widget.home.town.*;
	import b3.widget.home.user.*;
	import b3.widget.mail.*;
	import b3.widget.national.NationalIcon;
	import b3.widget.national.NationalStarPane;
	import b3.widget.task.*;
	import b3.widget.vip.*;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;

	public class HomeScene extends BasicScene
	{
		//事件（模式转换）
		public static const MODE_SWITCH:String = "ModeSwitch";
		
		//平常模式
		public static const NONEMODE:int = 0;
		//建造模式
		public static const BUILDINGMODE:int = 1;
		//拆除模式
		public static const REMOVEMODE:int = 2;
		//重造模式
		public static const REBUILDMODE:int = 3;
		//使用卡片场景
		public static const USECARDMODE:int = 4;
		//正在购买商品的场景
		public static const FINDPLACEMODE:int = 5;
		
		static private var instance:HomeScene;
		private var townLayer:Sprite;
		private var widgetLayer:Sprite;
		//用户头像
		public var playerPane:PlayerPane;
		//操作按钮
		public var tinyToolSet:SpriteSetBar;
		//任务图标
		public var taskSet:TaskSet;
		//占领者条
		public var occupantPane:OccupantPane;
		//我的占领数
		public var occupationCountPane:OccupationCountPane;
		//国庆五星条
		public var nationalStarPane:NationalStarPane;
		//体力条
		public var vitalityPane:VitalityPane;
		//神马币
		public var goldCoinPane:GoldCoinPane;
//		//神马元宝
//		public var goldIngotPane:GoldIngotPane;
		//中部信息条
		public var centerInfoPane:CenterInfoPane;
		//当前的视图显示比例
		private var _viewZoom:Number;
		//顶部条
		private var topPane:TopPane;
		//社交信息
		private var socialPane:SocialPane;
		//垃圾桶
		private var dustCan:DustCan;
		//回家按钮
		private var homeIcon:HomeIcon;
		//攻击按钮
		private var attackIcon:AttackIcon;
		//正在进行的操作模式
		private var operationMode:int;
		//购房卡使用时的临时变量
		public var tempBuildingModel:BuildingModel;
		//事件发布
		public var eventDispatcher:EventDispatcher;
		//
		public var cardItem:CardItem;
		
		public function HomeScene()
		{
			super();
			_viewZoom = 0.9;
			townLayer = new Sprite();
			addChild(townLayer);
			widgetLayer = new Sprite();
			addChild(widgetLayer);
			//顶部条
			topPane = new TopPane();
			widgetLayer.addChild(topPane);
			//系统图标组
			tinyToolSet = new SpriteSetBar();
			//加载放大图标
			var zoomInIcon:ZoomInIcon = new ZoomInIcon();
			tinyToolSet.setSprite(0, zoomInIcon);
			//加载缩小图标
			var zoomOutIcon:ZoomOutIcon = new ZoomOutIcon();
			tinyToolSet.setSprite(1, zoomOutIcon);
			//加载普通图标
			var normalScreenIcon:NormalScreenIcon = new NormalScreenIcon();
			tinyToolSet.setInvisibleSprite(2, normalScreenIcon);
			//加载全屏图标
			var fullScreenIcon:FullScreenIcon = new FullScreenIcon();
			tinyToolSet.setSprite(2, fullScreenIcon);
			//加载音乐图标
			var musicIcon:MusicIcon = new MusicIcon();
			tinyToolSet.setSprite(3, musicIcon);
			//加载声音图标
			var soundIcon:SoundIcon = new SoundIcon();
			tinyToolSet.setSprite(4, soundIcon);
			widgetLayer.addChild(tinyToolSet);
			
			//回家按钮
			homeIcon = new HomeIcon();
			widgetLayer.addChild(homeIcon);
			//攻击按钮
			attackIcon = new AttackIcon();
			widgetLayer.addChild(attackIcon);
			
			//垃圾按钮
			dustCan = new DustCan();
			widgetLayer.addChild(dustCan);
			
			//加载任务条
			var me:Player = PlayerManager.getInstance().me;
			
			//加载用户头像
			playerPane = new PlayerPane(me.userId);
			addChild(playerPane);
			
			taskSet = new TaskSet(me.taskList);
			widgetLayer.addChild(taskSet);
			//黄钻图标			
			var vipDailyGift:VipDailyGift = new VipDailyGift();
			vipDailyGift.x = 105;
			vipDailyGift.y = 175;
			widgetLayer.addChild(vipDailyGift);
			//国庆任务
			var nationalIcon:NationalIcon = new NationalIcon();
			nationalIcon.x = 145;
			nationalIcon.y = 145;
			widgetLayer.addChild(nationalIcon);
			//社交信息
			socialPane = new SocialPane();
			widgetLayer.addChild(socialPane);
			//占领者条
			occupantPane = new OccupantPane(); 
			occupantPane.anchor = BasicWidget.ANCHOR_RIGHT_TOP;
			occupantPane.x = -160;
			occupantPane.y = 0;
			widgetLayer.addChild(occupantPane);
			//我的占领城市数
			occupationCountPane = new OccupationCountPane(); 
			occupationCountPane.anchor = BasicWidget.ANCHOR_RIGHT_TOP;
			occupationCountPane.x = -160;
			occupationCountPane.y = 60;
			widgetLayer.addChild(occupationCountPane);
			//国庆五星
			nationalStarPane = new NationalStarPane();
			nationalStarPane.anchor = BasicWidget.ANCHOR_MIDDLE_TOP;
			nationalStarPane.x = -120;
			nationalStarPane.y = 20;
			widgetLayer.addChild(nationalStarPane);
			//体力条
			vitalityPane = new VitalityPane();
			vitalityPane.anchor = BasicWidget.ANCHOR_MIDDLE_TOP;
			vitalityPane.x = 0;
			vitalityPane.y = 20;
			widgetLayer.addChild(vitalityPane);
			//神马币
			goldCoinPane = new GoldCoinPane();
			goldCoinPane.anchor = BasicWidget.ANCHOR_MIDDLE_TOP;
			goldCoinPane.x = 120;
			goldCoinPane.y = 20;
			widgetLayer.addChild(goldCoinPane);
//			//神马元宝
//			goldIngotPane = new GoldIngotPane();
//			goldIngotPane.anchor = BasicWidget.ANCHOR_MIDDLE_TOP;
//			goldIngotPane.x = 160;
//			goldIngotPane.y = 48;
//			goldIngotPane.scaleX = 0.7;
//			goldIngotPane.scaleY = goldIngotPane.scaleX;
//			widgetLayer.addChild(goldIngotPane);
			//中央信息条
			centerInfoPane = new CenterInfoPane();
			widgetLayer.addChild(centerInfoPane);
			//加载底部面板
			BottomPane.getInstance().y = BFont.stageHeight;
			widgetLayer.addChild(BottomPane.getInstance());
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			eventDispatcher = new EventDispatcher();
		}
		
		private function onAddedToStage(event:Event):void
		{
			stage.addEventListener(Event.FULLSCREEN, onFullScreen);
			onFullScreen(null); 
		}

		private function onRemovedFromStage(event:Event):void
		{
			stage.removeEventListener(Event.FULLSCREEN, onFullScreen);
		}
	
		private function refreshWidgetPos():void
		{
			playerPane.x = 10;
			playerPane.y = 10;
			tinyToolSet.y = 200;
			tinyToolSet.x = BFont.stageWidth - 45;
			taskSet.y = 150;
			taskSet.x = 20;
			BottomPane.getInstance().hide();
		}
		
		private function onFullScreen(event:Event):void
		{
			var stage:Stage = SceneManager.getInstance().stage;
			if (stage.displayState != StageDisplayState.FULL_SCREEN && tinyToolSet.getInvisibleSprite(2) is FullScreenIcon)
			{
				tinyToolSet.exchangeSprite(2);
			}
			else if(stage.displayState == StageDisplayState.FULL_SCREEN && tinyToolSet.getInvisibleSprite(2) is NormalScreenIcon)
			{
				tinyToolSet.exchangeSprite(2);
			}
			refreshWidgetPos();
		}
		
		public function clearTownLayer():void
		{
			if (townLayer.numChildren > 0)
			{
				var town:Town = townLayer.getChildAt(0) as Town;
				townLayer.removeChildAt(0);
				town.destroy();
			}
		}
		
		public function loadPlayer(player:Player):void
		{
			//播放切换动画
			var gridShadowMovie:GridShadowMovie = new GridShadowMovie(BFont.stageWidth, BFont.stageHeight);
			gridShadowMovie.direction = GridShadowMovie.DIRECTION_LEFT;
			WidgetManager.getInstance().popOut([gridShadowMovie])
			BottomPane.getInstance().hide();
			//载入用户城镇
			clearTownLayer();
			var town:Town = new Town(player);
			townLayer.addChild(town);
			var me:Player = PlayerManager.getInstance().me;
			//好友
			if (player.userId != me.userId)
			{
				player.loadPlayerInfo(true);
				player.townModel.loadBuildings(true);
				player.packageModel.loadObjects(false, true);
				player.stateModel.loadState(true);
				homeIcon.show();
				topPane.hide();
				if (player.type != PlayerManager.CENTER_TYPE)
					attackIcon.show();
			}
			//自己
			else
			{
				player.loadPlayerInfo(true);
				player.townModel.loadBuildings(true);
				player.packageModel.loadObjects(true, true);
				player.stateModel.loadState(true);
				homeIcon.hide();
				topPane.show();
				attackIcon.hide();
			}
		}
		
		public function getCurrentTown():Town
		{
			return townLayer.getChildAt(0) as Town;
		}
		
		static public function getInstance():HomeScene
		{
			if (!instance)
				instance = new HomeScene();
			return instance;
		}
		
		public function getOperationMode():int
		{
			return operationMode;
		}
		
		public function setOperationMode(operationMode:int):void
		{
			this.operationMode = operationMode;
			if (operationMode == HomeScene.NONEMODE)
			{
				dustCan.hide();
				var me:Player = PlayerManager.getInstance().me;
				if (getCurrentTown().owner.userId != me.userId)
				{
					homeIcon.show();
					topPane.hide();
				}
				else
				{
					homeIcon.hide();
					topPane.show();
				}
				if (cardItem != null && cardItem.parent != null)
				{
					cardItem.parent.removeChild(cardItem);
				}
				cardItem = null;
			}
			else
			{
				dustCan.show();
				homeIcon.hide();
				topPane.hide();
			}
			eventDispatcher.dispatchEvent(new Event(MODE_SWITCH));
		}
		
		public function get zoom():Number
		{
			return _viewZoom;
		}
		
		public function set zoom(z:Number):void
		{
			_viewZoom = z;
			getCurrentTown().isoView.currentZoom = z;
		}
		
		//使用卡片		
		public function startToUseCard(cardModel:CardModel):void
		{
			var cardItem:CardItem = new CardItem(cardModel);
			HomeScene.getInstance().cardItem = cardItem;
			HomeScene.getInstance().setOperationMode(HomeScene.USECARDMODE);
			WidgetManager.getInstance().popOut([cardItem]);
		}
		
		//放置购买的房屋
		public function startToPlace(event:ModelEvent):void
		{
			TweenLite.delayedCall(0.1, getCurrentTown().startToPlace, [tempBuildingModel]);
		}
		
		//播放增加动画
		public function playCoinIncreaseMovie(coinImgName:String, coinNum:int, numColor:uint, startPos:Point, targetPos:Point):void
		{
			//大图标动画
			var GoldCoinIcon:Class = ResourceHelper.getInstance().getClassByName(coinImgName);
			var bigGoldCoin:BasicWidget = new BasicWidget();
			bigGoldCoin.addChild(new GoldCoinIcon());
			bigGoldCoin.x = startPos.x + 20;
			bigGoldCoin.y = startPos.y;
			var timeline:TimelineLite = new TimelineLite();
			timeline.append(new TweenLite(bigGoldCoin, 1.5, {y: startPos.y + 8, ease:Elastic.easeOut}));
			timeline.append(new TweenLite(bigGoldCoin, 0.5, {x: targetPos.x, y: targetPos.y, onComplete: bigGoldCoin.close}));
			//小图标动画
			var goldCoinWithNum:BasicWidget = new BasicWidget();
			var goldCoin:Sprite = new GoldCoinIcon();
			goldCoin.width = 20;
			goldCoin.scaleY = goldCoin.scaleX;
			goldCoinWithNum.addChild(goldCoin);
			var goldNumText:TextField = new TextField();
			goldNumText.selectable = false;
        	goldNumText.autoSize = TextFieldAutoSize.LEFT;
			goldNumText.defaultTextFormat = new TextFormat(BFont.fontName, 12, numColor);
			goldNumText.text = coinNum.toString();
			goldNumText.x = 22;
			goldCoinWithNum.addChild(goldNumText);
			goldCoinWithNum.x = startPos.x - 10;
			goldCoinWithNum.y = startPos.y;
			goldCoinWithNum.alpha = 0;
			timeline = new TimelineLite();
			timeline.append(new TweenLite(goldCoinWithNum, 1.5, {alpha: 1}));
			timeline.append(new TweenLite(goldCoinWithNum, 1, {y: startPos.y - 100, alpha: 0, onComplete: goldCoinWithNum.close}));
			WidgetManager.getInstance().popOut([bigGoldCoin, goldCoinWithNum]);
		}
	}
}